<script setup lang="ts">
// components
import comIcon from '@/components/common/com-icon/com-icon.vue';
import comNavigation from '@/components/common/com-navigationBar/com-navigation.vue';
// vue
import { useRoute } from 'vue-router';
// api
import { getPatientKnowledgeAPI, putPatientCommentAPI } from '@/api/articleDetail';
import { onMounted, ref, watch, watchEffect } from 'vue';
import type { ArticleDetailData } from '@/types/healthEncyclopedia';
import { postCollectAPI } from '@/api/healthEncyclopedia';
import { showToast } from 'vant';
import { postLikeAPI } from '@/api/doctorDetail';
// 第三方库 复制
import useClipboard from 'vue-clipboard3';
const { toClipboard } = useClipboard();
let route = useRoute()
let id = route.params.keyword as string
let data = ref<ArticleDetailData>()
const getPatientKnowledgeData = async () => {
  let res = await getPatientKnowledgeAPI(id)
  if (res.data.code === 10000) {
    data.value = res.data.data
  }
}
const contentTextRef = ref()
watchEffect(() => {
  if (contentTextRef.value && data.value?.content) {
    contentTextRef.value.innerHTML = data.value.content
  }
})
onMounted(() => {
  getPatientKnowledgeData()
})

const text = ref<string>('')
const disabled = ref<boolean>(false)
watch(text, () => {
  if (!text.value.trim().length) {
    disabled.value = true
  } else {
    disabled.value = false
  }
})
// 发布评论
const putPatientCommentData = async () => {
  let res = await putPatientCommentAPI(text.value, id)
  if (res.data.code === 10000) {
    showToast({
      message: "发布成功"
    })
    getPatientKnowledgeData()
  }
}
const send = () => {
  putPatientCommentData()
}
// 收藏
const onCollect = async () => {
  let res = await postCollectAPI('knowledge', id)
  if (res.data.code === 10000) {
    if (data.value!.collectionFlag === 0) {
      data.value!.collectionFlag = 1
      showToast({
        message: "收藏成功"
      })
    } else {
      data.value!.collectionFlag = 0
      showToast({
        message: "取消收藏成功"
      })
    }
  }

}
// 关注
const onFollow = async () => {
  let res = await postLikeAPI(id, 'doc')
  if (res.data.code === 10000) {
    if (data.value!.likeFlag === 1) {
      data.value!.likeFlag = 0
      showToast({
        message: "取消关注成功"
      })
    } else {
      data.value!.likeFlag = 1
      showToast({
        message: "关注成功"
      })
    }
  }
}
// 分享
const showShare = ref(false);
const options = [
  { name: '微信', icon: 'wechat' },
  { name: '朋友圈', icon: 'wechat-moments' },
];
type Option = {
  name: string, description: string, icon: string, className: string
}
const onSelect = async (option: Option, index: number) => {
  await toClipboard(window.location.href).then(res => {
    showToast({
      message: '复制链接成功,请打开微信进行分享'
    })
  }).catch(res => {
    console.log('失败');
  })
}

</script>
<template>
  <div class="box">
    <nav>
      <comNavigation height="44px">
        <template v-slot:title>
          <div class="title">
            文章详情
          </div>
        </template>
        <template v-slot:right>
          <div class="right">
            <div class="start">
              <comIcon class="startIcon" @click="onCollect"
                :name="data?.collectionFlag === 1 ? 'icons-home-onCollection' : 'icons-home-Collection'"></comIcon>
            </div>
            <div class="shared" @click="showShare = true">
              <comIcon class="sharedIcon" name="icons-home-share"></comIcon>
            </div>
          </div>
        </template>
      </comNavigation>
    </nav>
    <header class="headerArt">
      <p class="title">
        {{ data?.title }}
      </p>
      <p class="info">
        <span>{{ data?.createTime }}</span>|
        <span>{{ data?.readNumber }}次浏览</span>
      </p>
    </header>
    <main>
      <div class="content">
        <!-- 渲染富文本 -->
        <div class="contentText" ref="contentTextRef"></div>
        <div class="copyright">© 著作权归作者所有</div>
      </div>

      <div class="doctor">
        <img class="doctorAvatar" :src="data?.creatorAvatar" alt="">
        <div class="doctorInfo">
          <p>
            <span class="doctorName">{{ data?.creatorName }}</span>
            <span class="dep">{{ data?.creatorDep }} 丨 {{ data?.creatorTitles }}</span>
          </p>
          <p>
            <span class="hospatalGrade">{{ data?.hospatalGrade }}</span>
            <span class="hospitalName">{{ data?.creatorHospatalName }}</span>
          </p>
        </div>
        <div class="followBtn" @click="onFollow">
          {{ data?.likeFlag === 1 ? '已关注' : '关注' }}

        </div>

      </div>

      <div class="other-recommend">
        <div class="top">
          其他推荐
        </div>
        <div class="recommends">
          <div v-if="data?.recommends.length > 0" v-for="item in data?.recommends" class="recommend">推荐</div>
          <div v-else class="nomore">暂无</div>
        </div>
      </div>
      <div class="commentsBox">
        <div class="pox">
          <div class="top">
            评论（{{ data?.comments.length }}）
          </div>
          <div class="comments" v-for="item in data?.comments" :key="item.commentTime">
            <div class="commentUser">
              <span class="user">{{ item.commentUser }}</span>
              <span class="time">{{ item.commentTime }}</span>
            </div>
            <div class="commentContent">
              <p>{{ item.content }}</p>
            </div>
          </div>
        </div>
        <div class="comment-nomore">没有更多了</div>
      </div>
    </main>
    <div style="height: 60px;"></div>
    <footer>
      <div class="box">
        <input type="text" placeholder="发布你的评论" v-model="text">
        <button :disabled="disabled" class="button" @click="send">发布</button>
      </div>
    </footer>
  </div>
  <van-share-sheet v-model:show="showShare" :options="options" @select="onSelect" />
</template>
<style lang="scss" scpoed>
.box {
  nav {
    .title {
      font-size: 16px;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .right {
      margin-top: -2px;
      display: flex;
      justify-content: right;

      div {
        display: flex;
        align-items: center;
      }

      .start {

        margin-right: 20px;

        .startIcon {
          width: 20px;
          height: 20px;
        }
      }

      .shared {

        .sharedIcon {
          width: 25px;
        }
      }
    }
  }

  .headerArt {
    padding: 15px;
    border-bottom: 1px solid #f5f5f5;

    p {
      margin: 0;
    }

    .title {
      font-size: 18px;
    }

    .info {
      color: #c3c3c5;
      font-size: 11px
    }
  }

  main {
    background-color: #f6f7f9;

    .content {
      padding: 15px;
      background-color: #fff;
      margin-bottom: 15px;

      .contentText {
        font-size: 15px;
        font-weight: 400;

        p {
          margin: 0;
          line-height: 25px;

        }

        img {
          width: 100%;
        }

        ol,
        ul {
          padding-left: 20px;
          list-style: decimal;
          letter-spacing: 1px;

          li {
            margin-bottom: 10px;
          }
        }
      }

      .copyright {
        font-size: 12px;
        color: #c3c3c5;
        margin-top: 45px;
        text-align: center;
      }
    }

    .doctor {
      padding: 15px;
      background: #fff;
      display: flex;
      align-items: center;

      .doctorAvatar {
        width: 43px;
        border-radius: 50%;
        height: 43px;
        margin-right: 10px;
      }

      .doctorInfo {
        flex: 1;
        display: flex;
        justify-content: space-between;
        flex-direction: column;

        p {
          margin: 0;
          display: flex;
          align-items: center;

          .doctorName {
            font-size: 17px;
            margin-right: 10px;
          }

          .dep {
            font-size: 11px;
            color: #6f6f6f;
          }

          .hospatalGrade {
            display: inline-block;
            width: 25px;
            height: 14px;
            background: #677fff;
            border-radius: 2px;
            font-size: 9px;
            color: #fff;
            line-height: 14px;
            text-align: center;
            margin-right: 3px;
          }

          .hospitalName {
            font-size: 12px
          }
        }
      }

      .followBtn {
        padding: 0 10px;
        height: 30px;
        background-color: #e9f7f6;
        color: #2cb5a5;
        text-align: center;
        line-height: 30px;
        border-radius: 30px;
      }
    }

    .other-recommend {
      background-color: #fff;
      padding: 14px;

      .top {
        height: 24px;
        font-size: 15px;
        line-height: 24px;
      }

      .recommends {
        margin-top: 10px;
      }
    }

    .commentsBox {
      .pox {
        margin-top: 15px;
        padding: 15px 10px 0;
        background-color: #fff;

        .top {
          font-size: 15px;
        }

        .comments {
          border-bottom: 1px solid #e3e9ed;
          padding-bottom: 10px;

          .commentUser {
            display: flex;
            margin-top: 15px;
            justify-content: space-between;
            margin-bottom: 10px;

            .user {
              color: #777777;
            }

            .time {
              color: #c3c3c5;
            }
          }

          .commentContent {
            p {
              margin: 0;
            }
          }
        }
      }

      .comment-nomore {
        padding: 15px 0;
        text-align: center;
        background-color: #f6f7f9;
      }


    }
  }

  footer {
    width: 100%;
    box-sizing: border-box;
    padding: 10px 14px;
    position: fixed;
    bottom: 0;
    left: 0;
    height: 60px;
    background-color: #fff;

    .box {
      width: 100%;
      display: flex;
      justify-content: space-between;

      input {
        flex: 1;
        border: none;
        padding: 7px 0 7px 21px;
        height: 26px;
      }

      .button {
        width: 60px;
        height: 42px;
        text-align: center;
        line-height: 42px;
        color: #2cb5a5;
        border: none;
        background-color: #fff;
      }
    }
  }


}

.van-share-sheet__options {
  --van-share-sheet-cancel-button-height: 100px;
  justify-content: space-around !important;
  --van-share-sheet-icon-size: 50px;
  --van-share-sheet-option-name-font-size: 14px
}
</style>
